<template>
  <div id="activeTopSearch">
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="分享端：" prop="checkStatu">
          <el-select v-model="formInline.topPort" placeholder="">
            <el-option
              v-for="item in ChooseShareList"
              :key="item.key"
              :label="item.portName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="formInline.sourcePort" placeholder="">
            <el-option
              v-for="item in shareList"
              :key="item.key"
              :label="item.portName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期" prop="createTime">
          <el-date-picker
            value-format="yyyy-MM-dd"
            class="data-input"
            v-model="formInline.statisticDate"
            type="date"
            :clearable="false"
            placeholder="选择日期"
            :picker-options="pickerTopDataDay"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button type="success" icon="el-icon-search" @click="toSearch()">
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getPort } from '@/api/shareBackFlow/eachPortShare'
import { getDay } from '@/utils/yesterday'
import { deepClone } from '@/utils/deepClone'
export default {
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          // chooseShare: '',
          // shareItem: '',
          // createTime: ''
          topPort: '',
          sourcePort: '',
          statisticDate: ''
        }
      }
    }
  },
  data() {
    return {
      ChooseShareList: [],
      shareList: [{ id: '', portName: '全部' }],
      topName: '',
      rules: {
        activityId: {},
        type: {},
        createTime: ''
      },
      pickerTopDataDay: {
        disabledDate(time) {
          //昨天之后不可选
          const yesterday = new Date() - 3600 * 1000 * 24 * 1
          return time.getTime() > yesterday
        }
      }
    }
  },
  watch: {
    'formInline.topPort': {
      handler(newV, oldV) {
        if (newV) {
          this.ChooseShareList.forEach((item) => {
            if (item.id == newV) {
              this.shareList = [].concat(
                { id: '', portName: '全部' },
                item.childList
              )
              this.formInline.sourcePort = ''
            }
          })
        }
        if (newV === '') {
          //全部
          this.shareList = [{ id: '', portName: '全部' }]
          this.formInline.sourcePort = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.formInline.statisticDate = getDay(-1, '-')
    this.PortData()
  },
  methods: {
    PortData() {
      let params = { type: 0 }
      getPort(
        params,
        (res) => {
          let a = deepClone(res)
          let obj = { id: '', portName: '全部', childList: [] }
          a.unshift(obj)
          this.ChooseShareList = a
        },
        (res) => {
          this.$message({
            message: res.message,
            type: 'warning'
          })
        }
      )
    },

    /* 搜索 */
    toSearch(formName) {
      // this.formInline.statisticDate = this.dateFormat(this.formInline.statisticDate)
      this.$emit('toSearch')
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      this.setDefaultValue()
    },
    setDefaultValue() {
      this.formInline.sourcePort = ''
      this.formInline.topPort = ''
      this.formInline.statisticDate = getDay(-1, '-')
    }
  }
}
</script>

<style lang="less"></style>
